package com.wanli.agent.utils;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * FileName: DisplayUtil
 * Author: (客尘)衡文闯
 * Date: 2018/10/30 9:51
 * Description:
 */
public class DisplayUtil {
    private static DisplayMetrics metrics;

    public static void init(Application application) {
        if (metrics == null) {
            metrics = application.getResources().getDisplayMetrics();
        }
    }

    public static int getScreenHeight() {
        checkInit();
        return metrics.heightPixels;
    }

    public static int getScreenWidth() {
        checkInit();
        return metrics.widthPixels;
    }

    /**
     * px to dip
     *
     * @param pxValue
     * @return
     */
    public static int px2dip(float pxValue) {
        checkInit();
        return (int) (pxValue / metrics.density + 0.5f);
    }

    /**
     * dip to px
     *
     * @param dipValue
     * @return
     */
    public static int dip2px(float dipValue) {
        checkInit();
        return (int) (dipValue * metrics.density + 0.5f);
    }

    private static void checkInit() {
        if (metrics == null) {
            throw new NullPointerException("Place init DisplayUtil");
        }
    }

    /**
     * px to sp
     *
     * @param pxValue
     * @return
     */
    public static int px2sp(float pxValue) {
        checkInit();
        return (int) (pxValue / metrics.scaledDensity + 0.5f);
    }

    /**
     * sp to px
     *
     * @param spValue
     * @return
     */
    public static int sp2px(float spValue) {
        checkInit();
        return (int) (spValue * metrics.scaledDensity + 0.5f);
    }

    /**
     * @param context
     * @param fontSize
     * @return
     * @category 通过字符大小获取单个字符的Px
     */
    public static float getWidthFontSize(Context context, int fontSize) {
        TextView textView = new TextView(context);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        textView.setLayoutParams(layoutParams);
        textView.setTextSize(fontSize);
        textView.setText("单");
        return getcharacterWidth(textView);
    }

    /**
     * @param text
     * @return
     * @category 获取文本字符串的像素大小
     */
    public static float getcharacterWidth(TextView text) {
        if (null == text || "".equals(text)) {
            return 0f;
        }
        Paint paint = new Paint();
        paint.setTextSize(text.getTextSize() * text.getTextScaleX());
        float text_width = paint.measureText(text.getText().toString());
        return text_width;
    }

    public static void getScreenRect(Context ctx_, Rect outrect_) {
        Display screenSize = ((WindowManager) ctx_.getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();
        outrect_.set(0, 0, screenSize.getWidth(), screenSize.getHeight());
    }

    /**
     * 获取状态栏高度
     *
     * @param activity
     * @return
     */
    public static int getStatusHeight(Activity activity) {
        int statusHeight = 0;
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        statusHeight = rect.top;
        if (0 == statusHeight) {
            Class<?> localClass;
            try {
                localClass = Class.forName("com.android.internal.R$dimen");
                Object object = localClass.newInstance();
                int height = Integer.parseInt(localClass.getField("status_bar_height").get(object).toString());
                statusHeight = activity.getResources().getDimensionPixelSize(height);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return statusHeight;
    }

    /**
     * 判断是否显示了导航栏
     * (说明这里的context 一定要是activity的context 否则类型转换失败)
     *
     * @param context
     * @return
     */
    public static boolean isShowNavBar(Context context) {
        if (null == context) {
            return false;
        }
        /**
         * 获取应用区域高度
         */
        Rect outRect1 = new Rect();
        try {
            ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
        } catch (ClassCastException e) {
            e.printStackTrace();
            return false;
        }
        int activityHeight = outRect1.height();
        /**
         * 获取状态栏高度
         */
        int statuBarHeight = getStatusHeight((Activity) context);
        /**
         * 屏幕物理高度 减去 状态栏高度
         */
        int remainHeight = getRealHeight((Activity) context) - statuBarHeight;
        /**
         * 剩余高度跟应用区域高度相等 说明导航栏没有显示 否则相反
         */
        if (activityHeight == remainHeight) {
            return false;
        } else {
            return true;
        }

    }

    /**
     * 获取真实屏幕高度
     *
     * @return
     */
    public static int getRealHeight(Activity activity) {
        WindowManager wm = null;
        if (null == wm) {
            wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        }
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            wm.getDefaultDisplay().getSize(point);
        }
        return point.y;
    }


    /**
     * 隐藏键盘
     *
     * @param activity
     */
    public static void hideSoftInput(Activity activity, View view) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
